package main

import (
	"fmt"
	"net"
	"os"
	"time"
)

func main() {
	// 获取本机的名称
	name, err := os.Hostname()
	if err != nil {
		fmt.Println("Error getting hostname:", err)
		return
	}

	// 定义广播地址
	addr, err := net.ResolveUDPAddr("udp", "255.255.255.255:12345")
	if err != nil {
		fmt.Println("Error resolving UDP address:", err)
		return
	}

	// 创建UDP socket
	conn, err := net.DialUDP("udp", nil, addr)
	if err != nil {
		fmt.Println("Error dialing UDP:", err)
		return
	}
	defer conn.Close()

	// 发送广播消息
	for {
		_, err = conn.Write([]byte(name))
		if err != nil {
			fmt.Println("Error sending broadcast:", err)
			return
		}
		time.Sleep(1 * time.Second)
	}
}
